Back to course home
0% completed
Vote For New Content
Distributed Databases, Data Partitioning & Sharding
Fundamentals of Distributed Databases
Partitioning in Databases
Partitioning Types
Sharding in Databases
Distributed Query Processing
Quiz
Data Replication and Consistency Models
Replication in Databases
Replication Topologies
Understanding the Consistency Models in Databases
The CAP Theorem and Its Implications
Quiz
Consenus and Leader Election
Understanding the Consensus
Consensus algorithms
Leader Election Strategies
Quiz
Recovery and Fault Tolerance in Distributed Databases
Understanding the Fault Tolerance
Logging Mechanisms in Databases
Checkpointing and Recovery Basics
Coordinated Recovery in Distributed Systems
High Availability Through Replication and Failover
Ensuring Data Resilience: Redundancy, Mirroring, and Recovery
Quiz
NoSQL Databases and New Data Models
Introduction to NoSQL
Key-Value Stores
Document-Oriented Databases
Column-Family and Wide-Column Stores
Graph Databases
Quiz
Advanced Topics and Emerging Trends
In-Memory Databases
NewSQL Databases
Time-Series and Spatial Databases
Big Data and Distributed Processing Frameworks
Quiz
Database Design Case Studies
Quora System Design & Database Design
TikTok System Design & Database Design
Airbnb System Design & Database Design
Snapchat System Design & Database Design
Optimistic vs. Pessimistic Concurrency Control
Optimistic Concurrency Control (OCC)
OCC assumes that conflicts are rare and allows transactions to execute without restrictions until they are ready to commit. At the commit stage, the system checks for conflicts.
How OCC Works
- Transaction Execution: A transaction reads data and makes changes locally without acquiring locks.
- Validation Phase: Before committing, the database checks if any conflicts occurred (e.g., if another transaction modified the same data).
- Commit or Rollback:
- If no conflicts are detected, the transaction commits.
- If conflicts are detected, the transaction rolls back and retries.
Advantages of OCC
- No Lock Overhead: Transactions do not acquire locks, reducing system overhead.
- Better Performance: Ideal for systems with low contention, as most transactions succeed without conflicts.
- Higher Scalability: Works well in distributed systems where locking can be expensive.
Disadvantages of OCC
- Rollback Costs: Transactions may have to restart from scratch if conflicts are detected during validation.
- Inefficient in High Contention: Frequent conflicts lead to excessive rollbacks, reducing performance.
Use Cases
- Systems with low contention where conflicts are infrequent.
- Read-heavy workloads, such as reporting or analytics.
- Distributed systems, where locking is expensive.
Real-World Example
- User A and User B both read the same product inventory.
- User A updates the inventory count and submits the transaction.
- When User B tries to update the inventory, the system validates and detects the conflict, prompting User B to retry.
Pessimistic Concurrency Control (PCC)
PCC assumes that conflicts are likely and prevents them by acquiring locks on data as soon as a transaction accesses it.
How PCC Works
- Lock Acquisition: When a transaction reads or writes data, it acquires a lock to ensure no other transaction can access the same data concurrently.
- Conflict Prevention: Locks prevent other transactions from accessing the same data until the current transaction completes.
- Commit or Release: Once the transaction commits or rolls back, the locks are released.
Advantages of PCC
- Prevents Conflicts: Transactions are serialized, ensuring no interference.
- No Rollbacks: Conflicts are avoided upfront, eliminating the need for restarts.
- Consistency Guarantees: Ideal for critical systems where correctness is paramount.
Disadvantages of PCC
- Lock Overhead: Locking introduces significant overhead, especially in systems with many transactions.
- Deadlocks: Multiple transactions can block each other, requiring deadlock detection and resolution.
- Reduced Concurrency: Locking limits parallelism, impacting performance in high-concurrency systems.
Use Cases:
- Systems with high contention, where conflicts are frequent.
- Write-heavy workloads, such as banking or financial systems.
- Critical applications that require strict consistency, like inventory management.
Real-World Example
- User A starts editing a product's inventory, acquiring a lock on the product row.
- User B tries to access the same product but is blocked until User A completes the transaction and releases the lock.
Key Differences Between OCC and PCC
| Aspect | Optimistic Concurrency Control (OCC) | Pessimistic Concurrency Control (PCC) |
|---|---|---|
| Assumption | Conflicts are rare | Conflicts are frequent |
| Locks | No locks used | Locks are used |
| Performance | High in low-contention scenarios | High in high-contention scenarios |
| Rollback | Rollbacks occur if conflicts are detected | No rollbacks due to conflicts |
| Overhead | Low locking overhead | High locking overhead |
| Use Case | Read-heavy or low-contention workloads | Write-heavy or high-contention workloads |
Choosing Between OCC and PCC
| Criteria | Use OCC | Use PCC |
|---|---|---|
| System Contention | Low contention | High contention |
| Transaction Type | Read-heavy | Write-heavy |
| Failure Tolerance | High (rollbacks acceptable) | Low (strict consistency required) |
| System Type | Distributed systems or systems with low conflicts | Centralized systems with frequent conflicts |
Understanding the Concurrency Control
Lock-Based Concurrency Control
Mark as Completed
On this page
Optimistic Concurrency Control (OCC)
How OCC Works
Advantages of OCC
Disadvantages of OCC
Use Cases
Real-World Example
Pessimistic Concurrency Control (PCC)
How PCC Works
Advantages of PCC
Disadvantages of PCC
Use Cases:
Real-World Example
Key Differences Between OCC and PCC
Choosing Between OCC and PCC